var code; //在全局 定义验证码

function username_check() {
    //获取用户名文本框的内容
    var uname = username.value;
    //判断用户名是否为空
    if (!uname) {
        //若为空,将后面span标签赋值
        span_username.innerHTML = "用户名不能为空"
        //利用jquery修改span标签中文字颜色
        $(document).ready(function () {
            $("#span_username").css("color", "red")
        })
        return;
    } else if (uname.length > 10) {
        span_username.innerHTML = "用户名太长";
        $(document).ready(function () {
            $("#span_username").css("color", "red")
        })
        return;
    } else if (uname.length < 5) {
        span_username.innerHTML = "用户名太短";
        $(document).ready(function () {
            $("#span_username").css("color", "red")
        })
        return;
    } else {
        span_username.innerHTML = "用户名可用";
        $(document).ready(function () {
            $("#span_username").css("color", "green")
        })
    }
    //正则表达式
    var valid = /^\w+$/;
    if (!valid.test(uname)) {
        span_username.innerHTML = "用户名必须是数字、字母或下划线"
        $(document).ready(function () {
            $("#span_username").css("color", "red")
        })
        return;
    }
}


/*密码格式验证*/
function password_check() {
    var upwd_upwd = password.value;
    if (!upwd_upwd) {
        span_password.innerHTML = "密码为空";
        $(document).ready(function () {
            $("#span_password").css("color", "red")
        })
        return;
    } else if (upwd_upwd.length > 10) {
        span_password.innerHTML = "密码太长";
        $(document).ready(function () {
            $("#span_password").css("color", "red")
        })
        return;
    } else if (upwd_upwd.length < 6) {
        span_password.innerHTML = "密码太短"
        $(document).ready(function () {
            $("#span_password").css("color", "red")
        })
        return;
    } else {
        span_password.innerHTML = "密码可用";
        $(document).ready(function () {
            $("#span_password").css("color", "green")
        })
    }
}


/*验证两次密码是否相同*/
function repsword_check() {
    var check_c = repsword.value;
    if (check_c === password.value) {
        span_repassword.innerHTML = "密码可用";
        $(document).ready(function () {
            $("#span_repassword").css("color", "green")
        })
    } else {
        span_repassword.innerHTML = "两次密码不一致";
        $(document).ready(function () {
            $("#span_repassword").css("color", "red")
        })
        return;
    }
}

/*
* 验证码生成
*/

function createCode() {
    //初始化验证码
    code = "";
    var codeLength = 4;//验证码的长度
    var checkCode = document.getElementById("checkCode");
    checkCode.value = "";

    var selectChar = new Array(0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
        'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z',
        'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z');
    //随机获取验证码
    for (var i = 0; i < codeLength; i++) {
        var charIndex = Math.floor(Math.random() * 62);
        code += selectChar[charIndex];
    }
    //意外判断,当生成的验证码长度不等于规定的长度,则重新调用生成验证码的方法(一般来说没有大用)
    if (code.length != codeLength) {
        createCode();
    }
    //将验证码赋值给span标签
    checkCode.value = code;
}

//提交方法
function checkregisterform() {
    //获取验证码
    var inputCode = cl.value;
    if (inputCode.length <= 0) {
        span_cl.innerHTML = "必须输入验证码";
        $(document).ready(function () {
            $("#span_cl").css("color", "red")
        })
        return false;
    } else if (inputCode != code) {
        span_cl.innerHTML = "验证码不一致";
        $(document).ready(function () {
            $("#span_cl").css("color", "red")
        })
        createCode();
        return false;
    }
    //获取单选框
    var val=$('input:radio:checked').val();
    if(val==null){
        span_ag.innerHTML = "必须选中服务协议";
        $(document).ready(function () {
            $("#span_ag").css("color", "red")
        })
        return false;
    }
    return true;
}